﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace update
{
    public partial class UpdateEvent : Form
    {
        public UpdateEvent()
        {
            InitializeComponent();
            this.label1.Text = "正在更新!";
        }
        
           
         
        private delegate void SetPos(int ipos, string text);
        private void SetTextMessage(int ipos, string text)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMessage);
                this.Invoke(setpos, new object[] { ipos, text });
            }
            else
            {
                Console.WriteLine(text + "====text============");
                this.label1.Text = "正在下载:" + text;
                this.label1.Refresh();
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.progressBar1.Refresh();
            }
        }
        private void UpdateEvent_Load(object sender, EventArgs e)
        {
            UpdateDll.Update("-wait 5", (string updateCode, string updateStr, string updateResult) =>
            {
                //{"message":"\u0432\u05f0\u04c9\u0666","success":true}
                this.label1.Text = updateResult + "===" + updateStr;
            JObject value = JObject.Parse(updateResult);
                if (value["finish"] != null && "true".Equals(value["finish"].ToString().ToLower()))
                {
                    //杀死当前进程
                    Thread.Sleep(1000);
                    Process[] process = Process.GetProcesses();
                    foreach (Process prc in process)
                    {
                        if (prc.ProcessName == "update")
                        {
                            prc.Kill();
                        }
                    }
                }
            }, "update");
        }
    }
}
